/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.model.siege;

import com.aionengine.gameserver.controllers.observer.ActionObserver;
import com.aionengine.gameserver.controllers.observer.IActor;
import com.aionengine.gameserver.geoEngine.scene.Spatial;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.services.ShieldService;
import com.aionengine.gameserver.services.SiegeService;
import com.aionengine.gameserver.world.zone.ZoneInstance;
import com.aionengine.gameserver.world.zone.handler.ZoneHandler;
import javolution.util.FastMap;

/**
 * Shields have material ID 11 in geo.
 *
 * @author Rolandas
 */
public class SiegeShield implements ZoneHandler {

    FastMap<Integer, IActor> observed = new FastMap<Integer, IActor>();
    private Spatial geometry;
    private int siegeLocationId;
    private boolean isEnabled = false;

    public SiegeShield(Spatial geometry) {
        this.geometry = geometry;
    }

    public Spatial getGeometry() {
        return geometry;
    }

    @Override
    public void onEnterZone(Creature creature, ZoneInstance zone) {
        if (!(creature instanceof Player))
            return;
        Player player = (Player) creature;
        if (isEnabled || siegeLocationId == 0) {
            FortressLocation loc = SiegeService.getInstance().getFortress(siegeLocationId);
            if (loc == null || loc.getRace() != SiegeRace.getByRace(player.getRace())) {
                ActionObserver actor = ShieldService.getInstance().createShieldObserver(this, creature);
                if (actor instanceof IActor) {
                    creature.getObserveController().addObserver(actor);
                    observed.put(creature.getObjectId(), (IActor) actor);
                }
            }
        }
    }

    @Override
    public void onLeaveZone(Creature creature, ZoneInstance zone) {
        IActor actor = observed.get(creature.getObjectId());
        if (actor != null) {
            creature.getObserveController().removeObserver((ActionObserver) actor);
            observed.remove(creature.getObjectId());
            actor.abort();
        }
    }

    public void setEnabled(boolean enable) {
        isEnabled = enable;
    }

    public boolean isEnabled() {
        return isEnabled;
    }

    public int getSiegeLocationId() {
        return siegeLocationId;
    }

    public void setSiegeLocationId(int siegeLocationId) {
        this.siegeLocationId = siegeLocationId;
    }

    @Override
    public String toString() {
        return "LocId=" + siegeLocationId + "; Name=" + geometry.getName() + "; Bounds=" + geometry.getWorldBound();
    }

}
